package org.example;
import java.util.*;

public class CalculationSystem_V03 {
    static class Question {
        int a;
        int b;
        char op;
        int answer;

        Question(int a, int b, char op, int answer) {
            this.a = a;
            this.b = b;
            this.op = op;
            this.answer = answer;
        }

        // 重写equals和hashCode，用于去重（判断算式是否相同）
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Question question = (Question) o;
            return a == question.a && b == question.b && op == question.op;
        }

        @Override
        public int hashCode() {
            return Objects.hash(a, b, op);
        }
    }

    public static void main(String[] args) {
        Random random = new Random();
        Set<Question> questionSet = new HashSet<>(); // 用Set自动去重
        int addCount = 30, subCount = 20; // 加法30题，减法20题（比例3:2）

        // 生成加法题（保证不重复）
        while (questionSet.size() < addCount) {
            int a = random.nextInt(100) + 1;
            int b = random.nextInt(101 - a);
            Question q = new Question(a, b, '+', a + b);
            questionSet.add(q); // Set自动过滤重复
        }

        // 生成减法题（保证不重复）
        while (questionSet.size() < addCount + subCount) {
            int a = random.nextInt(100) + 1;
            int b = random.nextInt(a) + 1;
            Question q = new Question(a, b, '-', a - b);
            questionSet.add(q);
        }

        // 转换为List并打乱顺序
        List<Question> questionList = new ArrayList<>(questionSet);
        Collections.shuffle(questionList);

        // 输出结果
        System.out.println("加减法口算练习系统-v0.3");
        for (int i = 0; i < questionList.size(); i++) {
            Question q = questionList.get(i);
            System.out.printf("%d %c %d = %d\t", q.a, q.op, q.b, q.answer);
            if ((i + 1) % 4 == 0) System.out.println();
        }
    }
}